package com.opennetexam.manager.facecompare;

import com.opennetexam.manager.utils.DateUtil;
import org.springframework.http.HttpHeaders;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 签名头
 * Created by guxuyang on 13/07/2017.
 */
public class SignatureUtil {

    private SignatureUtil() {}

    public static Map<String, String> getHeaders(String appKey, String appSecret) {
        Map<String,String> headers = new HashMap();
        String timestamp = DateUtil.dateToString(new Date(), "YYYY-MM-dd'T'hh:mm:ss'Z'");
        String signatureNonce = getRandomString();
        String platform = "1";
        headers.put("appKey", appKey);
        headers.put("timestamp", timestamp);
        headers.put("signatureNonce", signatureNonce);
        headers.put("platform", platform);
        headers.put("signature", Md5.md5(String.format("timestamp=%s&signatureNonce=%s&platform=%s&appSecret=%s", timestamp, signatureNonce, platform, appSecret)));
        return headers;
    }

    public static String getRandomString() { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 18; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}
